//
//  V2LeftMenuView.m
//  V2EX
//
//  Created by 花菜ChrisCai on 2016/6/11.
//  Copyright © 2016年 Chris. All rights reserved.
//

#import "V2LeftMenuView.h"
#import "V2LeftMenuCell.h"
#import "V2LeftMenuModel.h"
static CGFloat const kTopHeight = 120;
static NSString * const menuCellIdentifier = @"CellIdentifier";
@interface V2LeftMenuView ()<UITableViewDelegate,UITableViewDataSource>
/** 头像 */
@property(nonatomic, weak)IBOutlet UIImageView *pofileImageView;
/** 表格 */
@property(nonatomic, weak)IBOutlet UITableView *tableView;
/** 表格数据 */
@property(nonatomic, strong) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@end
@implementation V2LeftMenuView
+ (instancetype)menuView
{
    return [V2LeftMenuView viewFromXib];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setup];
}

- (void)setup
{
    // 初始化头像
    [self setupPofileImageView];
    // 初始化表格控件
    [self setupTableView];
    
}

/**
 *  初始化头像
 */
- (void)setupPofileImageView
{
    self.pofileImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.pofileImageView.cornerRadius = 5;
    self.pofileImageView.borderColor = RGB(0x8a8a8a, 1.0);
    self.pofileImageView.borderWidth = 1.0f;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.pofileImageView addGestureRecognizer:tap];
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    V2LoginViewController *vc = [V2LoginViewController sharedLoginManager];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
}
/**
 *  初始化表格控件
 */
- (void)setupTableView
{
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([V2LeftMenuCell class]) bundle:nil] forCellReuseIdentifier:menuCellIdentifier];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY =  - scrollView.contentOffset.y - kTopHeight;
    CGFloat h = kTopHeight + offsetY;
    if (h < 0) {
        h = 0;
    }
    
    self.topHeight.constant = h;
   
}


#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    V2LeftMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:menuCellIdentifier];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(menuView:selectCellForRowAtIndexPath:)] ) {
        [self.delegate menuView:self selectCellForRowAtIndexPath:indexPath];
    }
}

#pragma mark -
#pragma mark - 懒加载
- (NSArray *)dataArray
{
    if (!_dataArray) {
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menu.plist" ofType:nil]];
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary * dict in tempArray) {
            [modelArray addObject:[V2LeftMenuModel menuModelWithDict:dict]];
        }
        _dataArray = [modelArray copy];
    }
    return _dataArray;
}

- (void)setSelectedIndex:(NSInteger)index
{
    [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
}
@end
